/*
 *  area.h
 *  
 *
 *  Created by Jeff McKinstry on 7/19/10.
 *  Copyright 2010 The Neurosciences Institute. All rights reserved.
 *
 */
#ifndef AREAS 
#define AREAS

#include <vector>
#include <string>
using namespace std;

class areas{
	
public:
	
	class area{
	public:
		
		area(){
			width_mm = 0;
			area_number = 0;
			x0=0.0f;
			y0=0.0f;
			area_name = "";
			N=0;
		}
		
		area( string iname, int iN, float iwidth_mm, int iarea_number, float ix0, float iy0){
			area_name=iname;
			N = iN;
			x0=ix0;
			y0=iy0;
			width_mm=iwidth_mm;
			area_number=iarea_number;
		};
		
		string area_name;
		float width_mm;
		int area_number;
		int N; // Actual number of neurons in area.  Assumed that this is a perfect square for now.
		float x0, y0;
	};
	
	// This must change as the format changes in make_microcircuitry.m
	void load_areas(string area_file){
		
		ifstream fin(area_file.c_str());
		
		string area_name;
		float x0, y0, width_mm;
		int N;
		
		// Read the area data
		int i = 0;
		while ( fin >> area_name >> N >> x0 >> y0 >> width_mm ){
			area_map[area_name] = area(area_name, N, width_mm, i, x0, y0);
			cout << area_name << "*****" << endl;
			i++;
		}
		N_areas= i;
		
		fin.close();
		
		
	}
	
	// Pre: load_areas() called.
	int get_N_areas(){
		return N_areas;
	}
	
	
	// Pre: load_areas() called.
	int get_N(string area_name){
		if( exists(area_name) ){
			return area_map[area_name].N;
		}
		else{
			return -1;
		}
	}
	
	
	
	// Pre: load_areas() called.
	bool exists(string area_name){
		
		return (area_map.find(area_name)!=area_map.end());
		
	}

	
	// Pre: load_areas() called.
	int get_area_number(string area_name){
		
		if(exists(area_name)){
			return get_area(area_name).area_number;
		}
		else{
			return -1;
		}
	}

	// Pre: load_areas() called, and area_number exists.
	string get_area_name(int area_number){
		
		return get_area(area_number).area_name;
	}
	
	// Pre: load_areas() called.
	area get_area(string area_name){
		return area_map[area_name];
	}

	area operator[](string area_name){
		return area_map[area_name];
	}
	
	
	
private:

	
	// Pre: load_areas() called, and area_number is a valid area number
	area get_area(int area_number){
		
		for( map<string,area>::iterator it = area_map.begin();it!=area_map.end();it++){
			if( it->second.area_number == area_number ){
				return it->second;
			}
		}
		area junk;		
		return junk;
	}
	
	
	map<string,area> area_map;

	int N_areas;
	
};

#endif
